package cms.service.access;

import cms.bean.ErrorView;
import cms.bean.user.AccessUser;
import cms.common.data.ApiResult;
import cms.common.data.ApiResultCode;
import cms.common.exception.ServiceException;
import cms.manage.CaptchaManage;
import cms.manage.SettingManage;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class CaptchaAccessService implements AccessService<String[]> {
    @Autowired
    CaptchaManage captchaManage;
    @Resource
    SettingManage settingManage;

    /**
     * 验证码访问
     *
     * @param accessUser 登录用户
     * @param act        类型
     * @param request
     * @param captchas   验证信息
     * @return
     */
    @Override
    public ApiResult<Boolean> access(AccessUser accessUser, String act, HttpServletRequest request, String... captchas) {
        if (ArrayUtils.isEmpty(captchas)) {
            return ApiResult.buildSuccess();
        }
        //验证码
        boolean isCaptcha = captchaManage.actIsCaptcha(accessUser, "topic");
        if (isCaptcha) {//如果需要验证码
            String captchaKey = captchas[0];
            //验证验证码
            if (captchaKey != null && !"".equals(captchaKey.trim())) {
                //增加验证码重试次数
                //统计每分钟原来提交次数
                Integer original = settingManage.getSubmitQuantity("captcha", captchaKey.trim());
                if (original != null) {
                    settingManage.addSubmitQuantity("captcha", captchaKey.trim(), original + 1);//刷新每分钟原来提交次数
                } else {
                    settingManage.addSubmitQuantity("captcha", captchaKey.trim(), 1);//刷新每分钟原来提交次数
                }

                String captchaValue = captchas[1];
                String _captcha = captchaManage.captchaGenerate(captchaKey.trim(), "");
                if (captchaValue != null && !"".equals(captchaValue.trim())) {
                    if (_captcha != null && !"".equals(_captcha.trim())) {
                        if (!_captcha.equalsIgnoreCase(captchaValue)) {
//                            error.put("captchaValue", ErrorView._15.name());//验证码错误
                            throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._15.name()));
                        }
                    } else {
//                        error.put("captchaValue",ErrorView._17.name());//验证码过期
                        throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._17.name()));
                    }
                } else {
//                    error.put("captchaValue",ErrorView._16.name());//请输入验证码
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._16.name()));
                }
                //删除验证码
                captchaManage.captchaDelete(captchaKey.trim());
            } else {
//                error.put("captchaValue", ErrorView._14.name());//验证码参数错误
                throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._14.name()));
            }
        }
        return ApiResult.buildSuccess(isCaptcha);
    }
}
